package cn.elead.chaos.sso.server.web.login.service;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import cn.elead.chaos.sso.core.user.LoginUser;
import cn.elead.chaos.sso.server.web.system.domain.SsoClient;
import cn.elead.chaos.sso.server.web.system.domain.SysUser;

@Service
public class RedisSaveService {

	@Autowired
	private StringRedisService<String, String> codeRedisService;

	@Autowired
	private StringRedisService<String, SsoClient> clientRedisService;

	@Autowired
	private StringRedisService<String, SysUser> sysUserRedisService;

	@Autowired
	private StringRedisService<String, LoginUser> userRedisService;

	@Autowired
	private StringRedisService<String, Set<String>> roleRedisService;

	@Autowired
	private StringRedisService<String, Set<String>> permsRedisService;

	@Autowired
	private StringRedisService<String, SysUser> pwdCodeRedisService;

	public static final String CACHE_CODE = "cache:sso:code:";

	public static final String CACHE_CLIENT = "cache:sso:client:";

	public static final String CACHE_SYS_USER = "cache:current:user:";

	public static final String CACHE_USER = "cache:sso:user:";

	public static final String CACHE_ROLE = "cache:sso:role:";

	public static final String CACHE_PERMS = "cache:sso:perms:";

	public static final String CACHE_PWD_CODE = "cache:sso:pwd:code:";

	public String getCode(String code) {
		return codeRedisService.get(CACHE_CODE + code);
	}

	public SsoClient getClient(String clientId) {
		return clientRedisService.get(CACHE_CLIENT + clientId);
	}

	public SysUser getSysUser(String userId) {
		return sysUserRedisService.get(CACHE_SYS_USER + userId);
	}

	public LoginUser getLoginUser(String token) {
		return userRedisService.get(CACHE_USER + token);
	}

	public Set<String> getRoles(String userId) {
		return roleRedisService.get(CACHE_ROLE + userId);
	}

	public Set<String> getPermss(String userId) {
		return permsRedisService.get(CACHE_PERMS + userId);
	}

	public SysUser getPwdCode(String token) {
		return pwdCodeRedisService.get(CACHE_PWD_CODE + token);
	}

	public void saveCodeToRedis(String code, String token) {
		codeRedisService.set(CACHE_CODE + code, token, 20, TimeUnit.SECONDS);
	}

	public void saveClientToRedis(String clientId, SsoClient ssoClient) {
		clientRedisService.set(CACHE_CLIENT+clientId, ssoClient, 7L, TimeUnit.DAYS);
	}

	@Async
	public void saveSysUserToRedis(String userId, SysUser user) {
		sysUserRedisService.put(CACHE_SYS_USER + userId, user);
	}

	@Async
	public void saveLoginUserToRedis(String token, LoginUser user) {
		userRedisService.put(CACHE_USER + token, user);
	}

	@Async
	public void saveRoleToRedis(String userId, Set<String> role) {
		roleRedisService.put(CACHE_ROLE + userId, role);
	}

	@Async
	public void savePermsToRedis(String userId, Set<String> perms) {
		permsRedisService.put(CACHE_PERMS + userId, perms);
	}

	@Async
	public void savePwdCodeToRedis(String code, SysUser user) {
		pwdCodeRedisService.set(CACHE_PWD_CODE + code, user, 3L, TimeUnit.DAYS);
	}

	@Async
	public void deleteSysUserFromRedis() {
		sysUserRedisService.deleteBykeys(CACHE_SYS_USER + "*");
	}

	@Async
	public void deleteLoginUserFromRedis() {
		userRedisService.deleteBykeys(CACHE_USER + "*");
	}

	@Async
	public void deleteLoginUserFromRedis(String token) {
		permsRedisService.delete(CACHE_USER + token);
	}

	@Async
	public void deleteRoleFromRedis() {
		roleRedisService.deleteBykeys(CACHE_ROLE + "*");
	}

	@Async
	public void deletePermsFromRedis() {
		permsRedisService.deleteBykeys(CACHE_PERMS + "*");
	}

}
